package com.qf.homework2.controller;

import com.qf.homework2.entity.User;
import com.qf.homework2.servlet.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/usersd")
public class UserController {

    @RequestMapping("/exist")
    public void existUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userServiceImpl =(UserService) context.getBean("userServiceImpl");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String value = req.getParameter("value");
        String result = null;
        if (userServiceImpl.usernameExist(value)) {
            result = "2";
        } else {
            result = "1";
        }
        resp.getWriter().println(result);
    }

    @RequestMapping("/no1")
    public String no1() {
        System.out.println("jinlaile");
        return "hi.html";
    }
}
